package com.example.task.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.icu.util.BuddhistCalendar;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.example.task.MainActivityTeacher;
import com.example.task.R;
import com.example.task.bean.Lecture;
import com.example.task.bean.Teacher;
import com.example.task.bean.User;
import com.example.task.bean.User_Signup;
import com.example.task.delegate.SimpleDelegateAdapter;
import com.example.task.http.Threads.ListStudentInfoThread;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;

public class ListStudentInfoActivity extends AppCompatActivity {


    private int imageViews = R.mipmap.img_head;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_student_info);
        SimpleDelegateAdapter<User_Signup> mNewsAdapter;
        SmartRefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(ListStudentInfoActivity.this);
        recyclerView.setLayoutManager(layoutManager);
        Intent intent = getIntent();
        //从Intent中取出Bundle
        Bundle bundle = intent.getExtras();
        Teacher user = (Teacher)bundle.getSerializable("Teacher");
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("username",user.getUsername());
        ListStudentInfoThread thread = new ListStudentInfoThread(builder);
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(thread.value);

        JsonArray obj = new Gson().fromJson(thread.value, JsonArray.class);
        List<User_Signup> userlist = new ArrayList<>();
        for(JsonElement jsonElement:obj){
            String iduser = jsonElement.getAsJsonObject().get("iduser").toString().replace("\"","");//学生id
            String username = jsonElement.getAsJsonObject().get("username").toString().replace("\"","");//学生id
            String password = jsonElement.getAsJsonObject().get("password").toString().replace("\"","");//学生密码
            String name = jsonElement.getAsJsonObject().get("name").toString().replace("\"","");//学生名字
            String email = jsonElement.getAsJsonObject().get("email").toString().replace("\"","");//学生邮箱
            String telephone = jsonElement.getAsJsonObject().get("telephone").toString().replace("\"","");//学生联系方式
            String birthday = jsonElement.getAsJsonObject().get("birthday").toString().replace("\"","");//注册时间
            String sex = jsonElement.getAsJsonObject().get("sex").toString().replace("\"","");//性别
            String initdate = jsonElement.getAsJsonObject().get("initdate").toString().replace("\"","");
            String classRoom = jsonElement.getAsJsonObject().get("classRoom").toString().replace("\"","");//学生班别
            String faculty = jsonElement.getAsJsonObject().get("faculty").toString().replace("\"","");//学生院系
            String imgpath = jsonElement.getAsJsonObject().get("imgpath").toString().replace("\"","");//头像
            String signupID = jsonElement.getAsJsonObject().get("signupID").toString().replace("\"","");//报名id
            String activityID = jsonElement.getAsJsonObject().get("activityID").toString().replace("\"","");//活动id
            String activityName = jsonElement.getAsJsonObject().get("activityName").toString().replace("\"","");//活动名
            String time = jsonElement.getAsJsonObject().get("time").toString().replace("\"","");//报名时间
            String qrStates = jsonElement.getAsJsonObject().get("qrStates").toString().replace("\"","");//签到状态
            User_Signup user1 = new User_Signup(Integer.parseInt(iduser),username, password, name, email, telephone,
                    birthday, sex, initdate, classRoom, faculty, imgpath, signupID, activityID, activityName, time, qrStates);
            userlist.add(user1);
        }
        mNewsAdapter = new SimpleDelegateAdapter<User_Signup>(R.layout.activity_list_studeny_info_item, new LinearLayoutHelper(),userlist) {

            @Override
            protected void bindData(@NonNull RecyclerViewHolder holder, int position, User_Signup user_signup) {
                if (user_signup != null) {;
                    holder.image(R.id.image1, imageViews);
                    holder.text(R.id.text1, user_signup.getUsername());
                    holder.text(R.id.text2, user_signup.getClassRoom());
                    holder.text(R.id.text3,user_signup.getFaculty());
                    holder.text(R.id.text4,user_signup.getTime());
                    holder.click(R.id.message, new View.OnClickListener() {
                        @SingleClick
                        @Override
                        public void onClick(View v) {
                            Bundle bundle1 = new Bundle();
                            Intent intent = new Intent(ListStudentInfoActivity.this, Student_MessageActivity.class);
                            bundle1.putSerializable("User_Signup", user_signup);
                            intent.putExtras(bundle1);
                            startActivity(intent);
                        }
                    });
                }
            }
        };
        recyclerView.setAdapter(mNewsAdapter);


    }
}